//
//  BLPhoneLiveRoomViewController.swift
//  BLLiveTelecast
//
//  Created by BigL055 on 16/6/17.
//  Copyright © 2016年 bigL.liveTelecast.com. All rights reserved.
//

import UIKit


class BLPhoneLiveRoomViewController: UIViewController {
    
    var roomID : String = ""
    var UrlStr : String = ""
    
    //暂停按钮
    var pauseBtn = UIButton()
    //返回按钮
    var backBtn = UIButton()
    //播放单利对象
    var displayerVC : BLPlayerViewControllerTool = BLPlayerViewControllerTool.shareInstance
    var displayerView : UIView? 
    
}

extension BLPhoneLiveRoomViewController{
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.whiteColor()
        navigationController?.navigationBarHidden = true
        getRoomData()
        setupUI()        
    }
    override func viewDidDisappear(animated: Bool) {
        super.viewDidDisappear(animated)
        navigationController?.navigationBarHidden = false
    }
}

// MARK: - UI界面配置
extension BLPhoneLiveRoomViewController{
    
    func setupUI() {
        setupdisplayView()
        setupBackBtn()
        setupPauseBtn()
    }
    ///设置播放界面
    func setupdisplayView() {
        
        displayerView  = displayerVC.view
        self.addChildViewController(displayerVC)
        
        view.addSubview(displayerView!)
        displayerView!.snp_makeConstraints { (make) in
            make.top.right.bottom.left.equalToSuperview()
        }
    }
    
    //设置暂停按钮
    func setupPauseBtn() {
        //暂停按钮
        pauseBtn = UIButton(type: .Custom)
        pauseBtn.sizeToFit()
        pauseBtn.setBackgroundImage(UIImage(named: "btn_v_pause_36x36_"), forState: UIControlState.Normal)
        pauseBtn.setBackgroundImage(UIImage(named: "btn_v_pause_h_36x36_"), forState: UIControlState.Highlighted)
        pauseBtn.addTarget(self, action: #selector(pauseBtnClick(_:)), forControlEvents: UIControlEvents.TouchUpInside)
        view.addSubview(pauseBtn)
        pauseBtn.snp_makeConstraints { (make) in
            make.bottom.equalToSuperview().offset(-10)
            make.left.equalToSuperview().offset(10)
            make.width.height.equalTo(BL_BtnWH)
        }
    }
    
    
    
    //设置返回上级按钮
    func setupBackBtn() {
        //暂停按钮
        backBtn = UIButton(type: .Custom)
        backBtn.sizeToFit()
        backBtn.setBackgroundImage(UIImage(named: "btn_round_back_32x32_"), forState: UIControlState.Normal)
        backBtn.setBackgroundImage(UIImage(named: "btn_round_back_h_32x32_"), forState: UIControlState.Highlighted)
        backBtn.addTarget(self, action: #selector(backBtnClick), forControlEvents: UIControlEvents.TouchUpInside)
        view.addSubview(backBtn)
        backBtn.snp_makeConstraints { (make) in
            make.top.equalToSuperview().offset(10)
            make.left.equalToSuperview().offset(10)
            make.width.height.equalTo(BL_BtnWH)
        }
    }
    
}


extension BLPhoneLiveRoomViewController{
    //暂停/播放视频
    @IBAction func pauseBtnClick(sender: UIButton) {
        
        if displayerVC.isPlay {
            displayerVC.player?.pause()
            pauseBtn.setBackgroundImage(UIImage(named: "btn_v_play_36x36_"), forState: UIControlState.Normal)
            pauseBtn.setBackgroundImage(UIImage(named: "btn_v_play_h_36x36_"), forState: UIControlState.Highlighted)
        }
        else{
            displayerVC.player?.play()
            pauseBtn.setBackgroundImage(UIImage(named: "btn_v_pause_36x36_"), forState: UIControlState.Normal)
            pauseBtn.setBackgroundImage(UIImage(named: "btn_v_pause_h_36x36_"), forState: UIControlState.Highlighted)
        }
        displayerVC.isPlay = !displayerVC.isPlay
        
    }
    
    //返回上级
    func backBtnClick() {
        navigationController?.navigationBarHidden = false
        navigationController?.popViewControllerAnimated(true)
    }
    
}

/// MARK: - 网络请求
extension BLPhoneLiveRoomViewController{
    
    func getRoomData() {
        print("roomID : \(roomID)")
        print("urlStr : \(UrlStr)")
        if roomID != "" {
            BLGetDouyuData.shareInstance.getRoomData(roomID: roomID) { (dataArray) in
                self.displayerVC.setPlayItem(dataArray.autoLiveUrl!)
                print(dataArray.autoLiveUrl!)
            }
        }
        else if UrlStr != "" {
            self.displayerVC.setPlayItem(UrlStr)
        }
    }
}